package com.example.no1admin.controller;

import com.example.no1admin.pojo.dto.picture.PictureAddNewDTO;
import com.example.no1admin.pojo.dto.picture.PictureEditDTO;
import com.example.no1admin.pojo.vo.picture.PictureListItemVO;
import com.example.no1admin.service.IPictureService;
import com.example.no1admin.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import java.util.List;

@Api(tags = "图片管理模块")
@RequestMapping("/pictures")
@Slf4j
@RestController
public class PictureController {
    @Autowired
    IPictureService pictureService;

    @ApiOperation("创建相册")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 100)
    @RequestMapping("/add-new")
    public JsonResult addNew(@RequestParam("files") MultipartFile picFile, Long albumId ){
        log.debug("接受到的请求参数:{}",picFile);
        log.debug("接收到的相册id:{}",albumId);
        String url = pictureService.addNew(picFile,albumId);
        return JsonResult.ok(url);
    }

    @ApiOperation("添加轮播图")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 110)
    @RequestMapping("/add-banner")
    public JsonResult addBanner(@RequestBody MultipartFile picFile){
        log.debug("接受到的请求参数:{}",picFile);
        String url = pictureService.addBanner(picFile);
        return JsonResult.ok(url);
    }


    @ApiOperation("删除图片")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{name}/delete")
    public JsonResult deleteByUrl(@PathVariable String name){
        log.debug("接收到删除图片的请求");
        pictureService.deleteByName(name);
        return JsonResult.ok();
    }

    @ApiOperation("删除图片")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id}/del")
    public JsonResult deleteById(@PathVariable Long id){
        log.debug("接收到删除图片的请求");
        pictureService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改图片")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult updateById(PictureEditDTO pictureEditDTO){
        log.debug("接收到修改图片的请求");
        pictureService.updateById(pictureEditDTO);
        return JsonResult.ok();
    }

    @ApiOperation("图片列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult list(){
        log.debug("接收到查询图片列表的请求");
        List<PictureListItemVO> pictures = pictureService.list();
        return JsonResult.ok(pictures);
    }

    @ApiOperation("相册中的图片列表")
    @ApiOperationSupport(order = 500)
    @GetMapping("/{albumId}/album")
    public JsonResult listByAlbum(@PathVariable Long albumId){
        log.debug("接收到查询相册中的图片列表的请求");
        List<PictureListItemVO> pictures = pictureService.listByAlbum(albumId);
        return JsonResult.ok(pictures);
    }

    @ApiOperation("相册中的图片列表")
    @ApiOperationSupport(order = 500)
    @GetMapping("/{albumName}/album-name")
    public JsonResult listByAlbumName(@PathVariable String albumName){
        log.debug("接收到查询相册中的图片列表的请求");
        List<PictureListItemVO> pictures = pictureService.listByAlbumName(albumName);
        return JsonResult.ok(pictures);
    }

}
